package com.robot.controller.tool;

import com.robot.model.Protocol;
import com.robot.service.DubboService;
import com.robot.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/9/8
 * Time: 15:02
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/dubbo")
public class DubboController {

    @Autowired
    DubboService dubboService;

    @RequestMapping("/dubboPage")
    public String dubboPage(ModelMap modelMap){
        modelMap.put("zkAddress","47.105.55.243:2181");
        ResponseMessage responseMessage = dubboService.interfaceList("47.105.55.243:2181");
        if(responseMessage.isSuccess()){
            modelMap.put("interfaceList",responseMessage.getData());
        }
        return "/tool/dubboPage";
    }


    @RequestMapping("/resolveIp")
    public @ResponseBody
    ResponseMessage resolveIp(String zkAddress,String interfaceName){
        if(StringUtils.isEmpty(zkAddress)){
            return ResponseMessage.errorResponse("zk地址不能为空");
        }
        if(StringUtils.isEmpty(interfaceName)){
            return ResponseMessage.errorResponse("接口名称不能为空");
        }
        return dubboService.resolveIp(zkAddress,interfaceName);
    }
    @RequestMapping("/resolveMethod")
    public @ResponseBody
    ResponseMessage resolveMethod(String ip,String interfaceName){
        if(StringUtils.isEmpty(ip)){
            return ResponseMessage.errorResponse("ip地址不能为空");
        }
        if(StringUtils.isEmpty(interfaceName)){
            return ResponseMessage.errorResponse("接口名称不能为空");
        }
        return dubboService.resolveMethod(ip,interfaceName);
    }
    @RequestMapping("/methodParamTemplates")
    public @ResponseBody
    ResponseMessage paramTemplates(String interfaceName,String ip,String interfaceMethod){
        return dubboService.paramTemplates(interfaceName,ip,interfaceMethod);
    }
    @RequestMapping("/invoke")
    public @ResponseBody String invoke(Protocol protocol){
        return dubboService.invoke(protocol);
    }
    @RequestMapping("/invokeForTestCase")
    public @ResponseBody String invokeForTestCase(Protocol protocol){
        return dubboService.invokeForTestCase(protocol);
    }
    @RequestMapping("/invokeForTestPlan")
    public @ResponseBody String invokeForTestPlan(Integer interfaceId,String param){
        return dubboService.invokeForTestPlan(interfaceId,param);
    }
}
